﻿/*

******************************************************

CreateDate  *  Author         *  Content

2008-8-15   *  donne.yang     *  The Configuration

******************************************************

NO   *  UpdateDate   *  Author        *  Content

*********************************************************

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Configuration;

using FoxConn.Lib.Dbo;

namespace ReportLostCard.Source
{
    class InformixUtil
    {
        public InformixUtil()
        { }

        public static bool InsertToBlackListAndChangeEcardInfo()
        {
            ArrayList al = new ArrayList();
            ArrayList alEcard = new ArrayList();
            string strSql, strEmployeeId;
            bool blnResult = false;
            bool blnChechExist;
            DBException dbex = new DBException();

            DataSet ds = OracleUtil.GetDimissionPerson();

            if (StaticUtil.IsDsHasData(ds))
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    strEmployeeId = ds.Tables[0].Rows[i][0].ToString().ToUpper();
                    blnChechExist = CheckPerson(strEmployeeId);

                    if (!Config.DEBUG.EndsWith("0"))
                    {
                        Console.WriteLine(strEmployeeId + " " + blnChechExist.ToString());
                        if (i % 15 == 0)
                        {
                            Console.Clear();
                        }
                    }

                    if (blnChechExist)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("INSERT INTO blacklist(cardid,cardfixid,date1,time1,cardstatus,flag,flag01,flag02,changedate)");
                        sb.Append(" select cardid,cardfixid,extend(current,year to day),'" + DateTime.Now.ToShortTimeString() +"','03','N','N','N',extend(current,year to day)");
                        sb.Append(" from ecard where employeeid = '" + strEmployeeId + "' and cardstatusid in('01','06','07')");

                        al.Add(sb.ToString());

                        strSql = "update ecard set cardstatusid='03',changedate = extend(current,year to day) where employeeid = '" + strEmployeeId + "' and cardstatusid in('01','06','07')";
                        alEcard.Add(strSql);
                    }
                }
            }

            Console.WriteLine(al.Count.ToString() + " records need update!");
            dbex.SaveErrorLog(al.Count.ToString() + " records need update!");

            if (al != null && al.Count > 0)
            {
                OdbcDBOpration odbcDBOpration = new OdbcDBOpration();
                blnResult = odbcDBOpration.ExecuteTransaction(al) && odbcDBOpration.ExecuteTransaction(alEcard);
            }

            if (blnResult)
            {
                Console.WriteLine("Table:blacklist " + al.Count.ToString() + " records updated! Table:ecard " + alEcard.Count.ToString() + " records updated!");
                dbex.SaveErrorLog("Table:blacklist " + al.Count.ToString() + " records updated! Table:ecard " + alEcard.Count.ToString() + " records updated!");
            }

            return blnResult;
        }

        public static bool CheckPerson(string EmployeeId)
        {
            bool blnResult = false;
            string strSql = "select cardid from ecard where employeeid = '" + EmployeeId + "' and cardstatusid in('01','06','07')";

            DBOperation dbo = new DBOperation(1);
            DataSet ds = dbo.GetResult(strSql);

            if (StaticUtil.IsDsHasData(ds))
            {
                blnResult = true;
            }

            return blnResult;
        }
    }
}
